package cn.gogpay.duoim.sdk.service;

import cn.gogpay.duoim.sdk.constant.IMApis;
import cn.gogpay.duoim.sdk.constant.IMRespCode;
import cn.gogpay.duoim.sdk.exception.IMSDKException;
import cn.gogpay.duoim.sdk.model.IMRespResult;
import cn.gogpay.duoim.sdk.model.UpdateSelfUserInfoReq;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import okhttp3.Request;

/**
 * 用户
 */
public class IMUserService {

    /**
     * 修改用户信息
     *
     * @param req 请求
     * @return 响应
     */
    public IMRespResult<String> updateUserInfo(String token, UpdateSelfUserInfoReq req) {
        String param = JSON.toJSONString(req);
        Request request = IMApiFactory.buildLoginRequest(token, param, IMApis.UPDATE_USER_INFO);
        String data = IMApiFactory.callRequest(request);

        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };

        IMRespResult<String> result;
        try {
            result = JSON.parseObject(data, typeReference);
        } catch (Exception e) {
            throw new IMSDKException(IMRespCode.parse(), e.getMessage());
        }
        return result;
    }
}
